package com.cola.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Grup:com.cola.aop
 * @Auther:cola
 * @Data:2021/4/7-2021-04-22:45
 * @version:1.0
 */
public class ProxyFactoryBean implements InvocationHandler {
    private Object target;
    private  Advice advice;
    public  Object getProxy(){
        Object proxy= Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this);
        return  proxy;

    }

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public Advice getAdvice() {
        return advice;
    }

    public void setAdvice(Advice advice) {
        this.advice = advice;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        advice.beforeAdvice();
        Object obj = method.invoke(target, args);
        advice.afterAdvice();
        return obj;
    }
}
